﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityLayer;

namespace DataLayer.DAO
{
    public class clsPersonalDAO
    {
        private BD_LEGAJOEntities ContextoDeObjs = new BD_LEGAJOEntities();
        public bool Insertar(PRM_PERSONAL Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("PRM_PERSONAL", Obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool Modificar(PRM_PERSONAL obj)
        {

            try
            {
                ContextoDeObjs.AttachTo("PRM_PERSONAL", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;

            }
        }
        public bool Eliminar(PRM_PERSONAL obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("PRM_PERSONAL", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public List<PRM_PERSONAL> listar()
        {
            List<PRM_PERSONAL> lista = new List<PRM_PERSONAL>();
            var q = from c in ContextoDeObjs.PRM_PERSONAL
                    select c;
            lista = q.ToList();
            return lista;
        }

        public IEnumerable<Object> listarAbogado(String cp_per_codigo = "")
        {
            IEnumerable<Object> lista = new List<Object>();
            try
            {
                var q = from c in ContextoDeObjs.PRM_PERSONAL
                        select new
                        {
                            c.Cp_per_codigo,
                            nom_abogado = c.Cp_per_apepat + " " + c.Cp_per_apemat + ", " + c.Cp_per_nombre
                        };

                if (cp_per_codigo != String.Empty) q = q.Where(f1 => f1.Cp_per_codigo == cp_per_codigo);

                lista = q.ToList();
            }
            catch (Exception)
            {
                lista = null;
                throw;
            }
            return lista;
        }
    }
}
